#自动生成功能已添加，可直接运行，并在输入阶段直接回车键自动生成随机卡号进行测试

import random
import sys
import math


def conclusion(a):  # 显示结果
    if a == 1:
        print('这是一组有效的信用卡号')
    else:
        print('这不是一组有效的信用卡号')


def random_card():  # 生成随机卡号
    a = random.randint(1, 3)
    number = ''
    if a == 1:      #第一种情况，生成16位随机数字
        i = 0
        while i < 16:
            digit = random.randint(0, 9)
            number += str(digit)
            i += 1

    if a == 2:     #第二种情况，生成四组被‘—’相连的数字
        i = 0
        while i < 19:
            if i in (4, 9, 14):
                digit = '-'
                number += digit
                i += 1

            digit = random.randint(0, 9)
            number += str(digit)
            i += 1

    if a == 3:     #第三种情况，生成四组被非正确符号相连的数字
        i = 0
        while i < 19:
            if i in (4, 9, 14):
                digit = ['/', '_', '*']
                n = random.randint(0, 2)
                number += digit[n]
                i += 1

            digit = random.randint(0, 9)
            number += str(digit)
            i += 1
    print(number)
    return number


def judge(x):  # 判断卡号是否有效
    star = ['4', '5', '6']
    fir_n = x[0]
    count = star.count(fir_n)
    if count == 0:
        return 0
    elif len(x) in (16, 19):
        ele_range = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-']
        for i in x:
            if i not in ele_range:
                return 0
        number = x.replace('-', '')
        same_ele = 1
        for i in range(len(number)):
            if number[i] == number[i - 1]:
                same_ele += 1
        if same_ele >= 4:
            return 0
        else:
            return 1
    else:
        return 0


if __name__ == '__main__':
    a = input('请输入识别卡号，或者直接按回车键生成随机卡号：')
    if len(a)==0:
        conclusion(judge(str(random_card())))
    else:
        conclusion(judge(a))



